package edu.pku.sei.transformation.editor.actions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.TreeViewer;

import edu.pku.sei.transformation.editor.views.icons.IconsProvider;

public class ChooseTreeViewInputAction extends Action {
	private TreeViewer tv;
	private Object element;
	private List input;
	
	public ChooseTreeViewInputAction(TreeViewer tv, Object element, String label) {
		super(label, AS_CHECK_BOX);
		this.tv = tv;
		this.element = element;
		
		if (tv.getInput() instanceof List) {
			input = (List) tv.getInput();
			if (input.contains(element))
				this.setChecked(true);
		}
		else
			input = new ArrayList();
		this.setImageDescriptor(IconsProvider.getImageDescriptor(IconsProvider.TREE_ICO));
		
		
	}

	@Override
	public void run() {
		if (isChecked()) {
			input.add(this.element);
		}
		else {
			input.remove(this.element);
		}
		tv.setInput(input);
	}
}
